﻿(function () {
    var appCalc = angular.module('calculationModule', []);

    appCalc.controller('calculator', function ($scope, Services, $filter) {
        $scope.calculations = {
            HDNOR: 0,
            ETBAnch: 0,
            HDATB: 0,
            CaseFour:0,
            LessShifting: 0,
            TotalTime: 0,
            LayTimeAllowed: 0,
            TimeDemmurage: 0,
            TotalTimeInUsed: 0,
            Word: '',
            Case: '',
            HDETD:''

        };

        $scope.initCalc = function (type, id) {
            $scope.type = type;
            if (id == undefined)
                $scope.id = $scope.dataItem.Id;
            else
                $scope.id = id;
            $scope.condition = '';
        };

        $scope.$on('updateCalc', function () {
            if ($scope.id == Services.calc.Id && $scope.type == Services.calc.type) {
                $scope.calcCondition(Services.calc.markers, Services.calc.ports);
            }
        });

        $scope.substractDates = function (dt1, dt2) {
            var result = parseFloat(((dt1.getTime() - dt2.getTime()) / (1000 * 3600)).toFixed(2));
            var zoneDiff = (dt1.getTimezoneOffset() - dt2.getTimezoneOffset())/60;
            return result - zoneDiff;
        };

        $scope.toFixed = function (value) {
            return value.toFixed(2);
        };

        $scope.calcCase1 = function (HDETD, Case) {
            var hd = $scope.port.ETD_ATDTime;
            var nor = $scope.port.ETA_NORTime;
            var etb = $scope.port.ETB_ATBTime;
            var aa = $scope.port.AATime;
            if (etb != null && aa != null && hd != null && nor != null) {
                $scope.calculations.HDETD = HDETD;
                $scope.calculations.Case = Case;
                $scope.condition = 1;
                $scope.calculations.HDNOR = $scope.substractDates(hd, nor) - 6;         //lol error no 6
                $scope.calculations.ETBAnch = $scope.substractDates(etb, aa);
                $scope.calculations.TotalTime = parseFloat(($scope.calculations.HDNOR - $scope.calculations.ETBAnch).toFixed(2));
            }
            else {
                $scope.condition = 0;
                $scope.calculations.TotalTime = 0;
            }
        };

        $scope.calcCase2 = function (HDETD) {
            var hd = $scope.port.ETD_ATDTime;
            var atb = $scope.port.ETB_ATBTime;
            var nor = $scope.port.ETA_NORTime;
            if (hd != null && atb != null && nor != null) {
                $scope.calculations.HDETD = HDETD;
                $scope.condition = 2;
                $scope.calculations.HDATB = $scope.substractDates(hd, atb);
                $scope.calculations.TotalTime = $scope.calculations.HDATB;
            }
            else {
                $scope.condition = 0;
                $scope.calculations.TotalTime = 0;
            }
        };

        $scope.calcCase4and5 = function (Case, cond, HDETD) {
            var hd = $scope.port.ETD_ATDTime;
            var nor = $scope.port.ETA_NORTime;
            var aa = $scope.port.AATime;
            var flag = true;
            if (aa == null || $scope.port.AAMarker == '')
                flag = false;
            var cases = $scope.port.ETB_ATBTime;
            $scope.calculations.HDETD = HDETD;
            if (hd != null && cases != null && nor != null) {
                $scope.condition = cond;
                if ($scope.port.FinalCalculationOption == 1) {
                    $scope.calculations.CaseFour = $scope.substractDates(hd, nor);
                    $scope.calculations.Word = 'NOR';
                }
                else {
                    $scope.calculations.CaseFour = $scope.substractDates(hd, cases);
                    $scope.calculations.Word = Case;
                }
                $scope.calculations.LessShifting = 0;
                if (flag)
                    $scope.calculations.LessShifting = $scope.substractDates(cases, aa);
                $scope.calculations.TotalTime = parseFloat(($scope.calculations.CaseFour - $scope.calculations.LessShifting).toFixed(2));
                $scope.calculations.Case = Case;
            }
            else {
                $scope.condition = 0;
                $scope.calculations.TotalTime = 0;
            }
        };

        $scope.calcCondition = function (markers, ports) {
            if (ports.length == 0) {
                $scope.condition = 0;
                return;
            }
            if (markers != undefined) {
                if (markers.AAMarker == null || markers.AAMarker == undefined)
                    markers.AAMarker = '';
                $scope.port = markers;
                switch (markers.ETAMarker + markers.AAMarker + markers.ETBMarker + markers.ETDMarker) {
                    case 'NOR+6hAnchor aweighETBHD':
                        $scope.calcCase1('HD', 'ETB');
                        break;
                    case 'NOR+6hAnchor aweighETBETD':
                        $scope.calcCase1('ETD', 'ETB');
                        break;
                    case 'NOR+6hAnchor aweighATBHD':
                        $scope.calcCase1('HD', 'ATB');
                        break;
                    case 'NOR+6hAnchor aweighATBETD':
                        $scope.calcCase1('ETD', 'ATB');
                        break;
                    case 'NOR+6hATBHD':
                        $scope.calcCase2('HD');
                        break;
                    case 'NOR+6hATBETD':
                        $scope.calcCase2('ETD');
                        break;
                        //case 'NOR+6hETBETD':
                        //    $scope.condition = 3;
                        //    break;
                    case 'NORATBHD':
                        $scope.calcCase4and5('ATB', 5, 'HD');
                        break;
                    case 'NORATBETD':
                        $scope.calcCase4and5('ATB', 5, 'ETD');
                        break;
                    case 'NORETBHD':
                        $scope.calcCase4and5('ETB', 4, 'HD');
                        break;
                    case 'NORETBETD':
                        $scope.calcCase4and5('ETB', 4, 'ETD');
                        break;
                    case 'NORAnchor aweighETBHD':
                        $scope.calcCase4and5('ETB', 4, 'HD');
                        break;
                    case 'NORAnchor aweighETBETD':
                        $scope.calcCase4and5('ETB', 4, 'ETD');
                        break;
                    case 'NORAnchor aweighATBHD':
                        $scope.calcCase4and5('ATB', 5, 'HD');
                        break;
                    case 'NORAnchor aweighATBETD':
                        $scope.calcCase4and5('ATB', 5, 'ETD');
                        break;
                        //case 'ETAAnchor aweighETBHD':
                        //    $scope.condition = 6;
                        //    break;
                    default: $scope.condition = 0; break;
                }
                Services.commonCalc({ Id: $scope.id, type: $scope.type, ports: ports });
            }
        };

        $scope.getCondition = function (markers) {
            if (markers.ETDMarker == 'HD' && markers.ETD_ATDTime != null)
                return 1;
            var Case = 0;
            if (markers.AAMarker == null || markers.AAMarker == undefined)
                markers.AAMarker = '';
            switch (markers.ETAMarker + markers.AAMarker + markers.ETBMarker + markers.ETDMarker) {
                case 'NOR+6hAnchor aweighETBHD':
                    Case = 1; break;
                case 'NOR+6hAnchor aweighETBETD':
                    Case = 1; break;
                case 'NOR+6hAnchor aweighATBHD':
                    Case = 1; break;
                case 'NOR+6hAnchor aweighATBETD':
                    Case = 1; break;
                case 'NOR+6hATBHD':
                    Case = 2; break;
                case 'NOR+6hATBETD':
                    Case = 2; break;
                case 'NORETBHD':
                    Case = 4; break;
                case 'NORETBETD':
                    Case = 4; break;
                case 'NORAnchor aweighETBHD':
                    Case = 4; break;
                case 'NORAnchor aweighETBETD':
                    Case = 4; break;
                case 'NORAnchor aweighATBHD':
                    Case = 5; break;
                case 'NORAnchor aweighATBETD':
                    Case = 5; break;
                case 'NORATBHD':
                    Case = 5; break;
                case 'NORATBETD':
                    Case = 5; break;
                case 'ETAAnchor aweighATBHD':
                    Case = 6;
                    break;
                default: return 0;
            }
            if ($scope.isFullfilled(markers))
                return Case;
            if ($scope.isFullfilledCase(markers) && (Case == 2 || Case == 3))
                return Case;
            return 0;
        };

        $scope.isFullfilled = function (port) {
            var HD = port.ETD_ATDTime;
            var NOR = port.ETA_NORTime;
            var AA = port.AATime;
            var CASES = port.ETB_ATBTime;
            return HD != undefined && NOR != undefined && CASES != undefined && AA != undefined;
        };
        $scope.isFullfilledCase = function (port) {
            var HD = port.ETD_ATDTime;
            var NOR = port.ETA_NORTime;
            var CASES = port.ETB_ATBTime;
            return HD != undefined && NOR != undefined && CASES != undefined;
        };

        $scope.findElement = function (childID) {
            var obj = document.getElementById($scope.type + $scope.id);
            if (obj != null) {
                var elms = obj.getElementsByTagName("*");
                for (var i = 0; i < elms.length; i++) {
                    if (elms[i].id === childID)
                        return elms[i];
                }
            }
            return null;
        };

        $scope.getDateTimeString = function (date) {
            if (date === null)
                return '';
            return $filter('date')(date, 'dd.MM.yyyy HH:mm');
        };

        //$scope.parseDateTime = function (value) {
        //    if (value.indexOf('/') > -1)
        //        return kendo.parseDate(value, 'dd/MM/yyyy HH:mm');
        //    if (value.indexOf('-') > -1)
        //        return kendo.parseDate(value, 'dd-MM-yyyy HH:mm');
        //    if (value.indexOf('.') > -1)
        //        return kendo.parseDate(value, 'dd.MM.yyyy HH:mm');
        //    if (value.indexOf(',') > -1)
        //        return kendo.parseDate(value, 'dd,MM,yyyy HH:mm');
        //    return kendo.parseDate(value, 'dd MM yyyy HH:mm');
        //};

        //$scope.parseDate = function (value) {
        //    if (value.indexOf('/') > -1)
        //        return kendo.parseDate(value, 'dd/MM/yyyy');
        //    if (value.indexOf('-') > -1)
        //        return kendo.parseDate(value, 'dd-MM-yyyy');
        //    if (value.indexOf('.') > -1)
        //        return kendo.parseDate(value, 'dd.MM.yyyy');
        //    if (value.indexOf(',') > -1)
        //        return kendo.parseDate(value, 'dd,MM,yyyy');
        //    return kendo.parseDate(value, 'dd MM yyyy');
        //};
    });

    appCalc.controller('totalController', function ($scope, Services) {
        $scope.total = {
            load: 0,
            isLoad: false,
            discharge: 0,
            isDisch: false
        };

        $scope.layTimeNum = 0;
       
        $scope.initTotal = function (loadPorts, dischPorts, id) {
            $scope.id = id;
            var lay = 0;
            if (loadPorts.length > 0)
                lay = loadPorts[0].Laytime;
            if (dischPorts.length > 0)
                lay = dischPorts[0].Laytime;
            if (lay == undefined) 
                lay = 0;
            $scope.lay = { Time: lay.toString(), TimeNum: lay };
        };

        $scope.$on('updateTotal', function () {
            if ($scope.dataItem.Id == Services.total.Id ) {
                if (Services.total.type == 'load') {
                    $scope.total.load = Services.total.total;
                    $scope.total.isLoad = Services.total.flag;
                }
                else {
                    $scope.total.discharge = Services.total.total;
                    $scope.total.isDisch = Services.total.flag;
                }
            }
        });

        $scope.toFixed = function (value) {
            return value.toFixed(2);
        };

        $scope.isComplete = function () {
            return $scope.total.isLoad || $scope.total.isDisch;
        };

        $scope.pattern = /^[0-9]{1,5}$/;

        $scope.checkStatus = function (value) {
            return value == 'Accepted' || value == 'Invoiced';
        };

        $scope.changeSelectionLay = function (keyEvent, Id) {
            if (keyEvent.which == 9) {
                $('#nameload' + Id).focus();
                $('#nameload' + Id).select();
                keyEvent.preventDefault();
            }
        };

        $scope.check = function (loadPorts, dischPorts) {
            if ($scope.lay.Time == '' || $scope.lay.Time == '0') {
                $scope.lay.TimeNum = 0;
                $scope.lay.Time = '0';
            }
            if ($scope.lay.Time.charAt(0) == '0') 
                $scope.lay.Time = $scope.lay.Time.substr(1, $scope.lay.Time.length);
            if ($scope.lay.Time.match($scope.pattern) !== null)
                $scope.lay.TimeNum = parseInt($scope.lay.Time);
            else
                $scope.lay.Time = String($scope.lay.TimeNum);
            $scope.assignLay(loadPorts);
            $scope.assignLay(dischPorts);
            Services.boxCheck({ id: $scope.id });
        };

        $scope.assignLay = function (array) {
            for (var i = 0; i < array.length; i++)
                array[i].Laytime = $scope.lay.TimeNum;
        };
    });
})()